﻿using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using NET4.DataNET4;
using NET4.Database;
using NET4.DataNET4.Repository;
using System.Globalization;
using System.Data.Linq;

public partial class _Default : System.Web.UI.Page 
{
    private Offerte offerte;

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            string strId = AES.Decrypt(this.Request["id"].ToString(), "net4");
            if (strId.Trim() == string.Empty)
            {
                this.Redirect();
            }
            else
            {
                int id;
                if (int.TryParse(strId, out id))
                {
                    this.offerte = OfferteRepository.Instance.Select(id);
                    if (this.offerte.OfferteAanvraag.UitersteReactieDatum.CompareTo(DateTime.Now) > 0)
                    {
                        this.Init();
                    }
                    else
                    {
                        this.Response.Redirect("./RequestExpired.htm");
                    }
                }
                else
                {
                    this.Redirect();
                }
            }
        }
        catch (Exception ex)
        {
            //this.Response.Write(HttpUtility.UrlEncode(AES.Encrypt("1", "net4")) + "<br />");
            //this.Response.Write(System.Drawing.Imaging.ImageFormat.Gif.ToString() + "<br />");
            //this.Response.Write(System.Drawing.Imaging.ImageFormat.Png.ToString() + "<br />");
            //this.Response.Write(System.Drawing.Imaging.ImageFormat.Jpeg.ToString() + "<br />");
            //this.Response.Write(this.MapPath("./") + "<br />");
            this.Redirect();
            Console.WriteLine(ex.Message);
        }
    }

    private void Redirect() {
        this.Response.Redirect("http://lmgtfy.com/?q=how-to+hack");
    }

    private new void Init()
    {
        this.Image.ImageUrl = this.ResolveUrl("Image.aspx?id=" + this.offerte.OfferteAanvraagId);
        this.litOmschrijving.Text = this.offerte.OfferteAanvraag.Omschrijving;
        this.litAantal.Text = this.offerte.OfferteAanvraag.Aantal.ToString();
        if (!this.IsPostBack)
        {
            this.txtPrijsPerStuk.Text = this.offerte.PrijsPerStuk != null ? decimal.ToDouble((decimal)this.offerte.PrijsPerStuk).ToString("N", new CultureInfo("en-US")) : "";
            this.txtOpmerking.Text = this.offerte.Opmerking;
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        decimal prijsPerStuk;
        if (decimal.TryParse(this.txtPrijsPerStuk.Text, NumberStyles.Number, new CultureInfo("en-US"), out prijsPerStuk))
        {
            //To insert test image
            //this.offerte.OfferteAanvraag.Afbeelding = new Binary(Utils.ReadByteArrayFromFile(this.MapPath(@"Images/NA.gif")));
            //OfferteAanvraagRepository.Instance.Save(this.offerte.OfferteAanvraag);
            this.offerte.PrijsPerStuk = prijsPerStuk;
            this.offerte.Opmerking = this.txtOpmerking.Text;
            OfferteRepository.Instance.Save(this.offerte);
        }
        else
        {
            Response.Write("Oops!! Something went wrong.");
        }
    }
}
